package com.onpositive.wiki3.db.propaccess;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.onpositive.wiki3.db.impl.WikiDBImpl;

public class PropPackager {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			// CategoryAccessImpl mm = (CategoryAccessImpl) wikiDBImpl
			// .getCategoryAccessor();
			DataInputStream ss1 = new DataInputStream(new BufferedInputStream(
					new FileInputStream(wikiDBImpl.getPath()
							+ "/props/props.dt")));
			IntObjectOpenHashMap<HashMap<String, String>> omap = new IntObjectOpenHashMap<HashMap<String, String>>();
			HashMap<String, DataOutputStream> ms = new HashMap<String, DataOutputStream>();
			HashMap<String, ByteArrayOutputStream> bs = new HashMap<String, ByteArrayOutputStream>();
			try {
				int zq = 0;
				while (true) {
					String propName = ss1.readUTF();
					int readInt = ss1.readInt();
					String readUTF = ss1.readUTF();
					zq++;
					if (zq % 10000 == 0) {
						System.out.println(zq);
					}
					DataOutputStream dataOutputStream = ms.get(propName);
					if (dataOutputStream == null) {
						ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
						bs.put(propName, byteArrayOutputStream);
						DataOutputStream value = new DataOutputStream(
								byteArrayOutputStream);
						ms.put(propName, value);
						dataOutputStream = value;
					}
					dataOutputStream.writeInt(readInt);
					dataOutputStream.writeUTF(readUTF);

					// HashMap<String, String> hashMap = omap.get(readInt);
					// if (hashMap == null) {
					// hashMap = new HashMap<String, String>();
					// omap.put(readInt, hashMap);
					// }
					// hashMap.put(propName, readUTF);
				}
			} catch (EOFException e) {
				// e.printStackTrace();
			}
			BufferedOutputStream dd = new BufferedOutputStream(
					new FileOutputStream(new File(
							"C:/work/content/props/byprop.dat")));
			HashMap<String, Integer> mm = new HashMap<String, Integer>();
			DataOutputStream propsList = new DataOutputStream(dd);
			for (String s : ms.keySet()) {
				DataOutputStream dataOutputStream = ms.get(s);
				dataOutputStream.close();
				ByteArrayOutputStream byteArrayOutputStream = bs.get(s);
				byte[] byteArray = byteArrayOutputStream.toByteArray();
				mm.put(s, propsList.size());
				propsList.writeInt(byteArray.length);
				propsList.write(byteArray);
			}
			propsList.close();
			ObjectOutputStream os = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(
							"C:/work/content/props/byprop.cat")));
			os.writeObject(mm);
			os.close();
			System.out.println("Done");
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}
